package com.cloudfun.campusshare.listener;

import com.cloudfun.campusshare.common.constant.RabbitConstants;
import com.cloudfun.campusshare.service.DeviceAlarmService;
import com.cloudfun.msgsubscribe.DeviceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * Created by Huqin on 2020/3/31
 */
@Slf4j
@Component
@RabbitListener(queues = RabbitConstants.Queue.DEVICE_FAULT)
public class DeviceFaultMsgListener extends AbstractDeviceMsgListener {

    @Autowired
    private DeviceAlarmService deviceAlarmService;

    @Override
    protected void doProcess(DeviceInfo deviceInfo) {
        log.info("收到2G设备告警消息：{}", deviceInfo);
        Set<String> alarms = deviceInfo.getAlarms();
        if (alarms.contains("alarmCancel")) {
            deviceAlarmService.onAlarmCancel(deviceInfo.getImei());
            return;
        }
        for (String alarm : alarms) {
            deviceAlarmService.onAlarm(deviceInfo.getImei(), alarm);
        }
    }

}
